package uit.edu.vn;

import java.util.ArrayList;
import java.util.List;


import android.app.ListActivity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ListSMSActivity extends ListActivity {
    /** Called when the activity is first created. */
	 /** Called when the activity is first created. */
	private Cursor myCursor;

    private ContentResolver cr; 
	private ListView listSMS;
	private List<mySMS> listSms = new ArrayList<mySMS>();
	private ArrayList<Boolean> itemSMSCheck=new  ArrayList<Boolean>(); 
	private ArrayList<String> itemSMSString= new ArrayList<String>();
	private SMSListAdapter mAdapter ;

    private final int menu_Logout=0;
    private final int menu_Backup=1;
    private final int menu_MarkAll=2;
    private String[] smsName ;
    private String[] smsBody ;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listSMS =getListView();
        listSMS.setChoiceMode(2);
        cr=getContentResolver();
        Uri mInboxURI = Uri.parse("content://sms/inbox"); 
      //  myCursor=managedQuery(mInboxURI, null, null, null, null);
        myCursor=cr.query(mInboxURI, null, null, null, null);
        startManagingCursor(myCursor);
        smsName=new String[myCursor.getCount()];
        smsBody= new String[myCursor.getCount()];
        while(myCursor.moveToNext())
        {
        	for(int i=0; i<myCursor.getCount();i++)
        	{
        		smsName[i] = myCursor.getString(myCursor.getColumnIndexOrThrow("address")).toString();
            	smsBody[i]=myCursor.getString(myCursor.getColumnIndexOrThrow("body")).toString();
            //	String name=smsName[i];
        	}
        	
        //	Log.d("1", smsName);
        //	Log.d("0", smsBody);
        	listSms.add(new mySMS(smsName.toString(), smsBody.toString()));
        }
     
       mAdapter= new SMSListAdapter(this,listSms);
       this.setListAdapter(mAdapter); 
       // lay trang thai cac checkbox
       itemSMSCheck=mAdapter.getCheckList();
       listSMS.setOnItemClickListener(new OnItemClickListener() 
       {
    	   @Override
    	   public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
    		   // TODO Auto-generated method stub

    	        int index = myCursor.getColumnIndexOrThrow("address");
    	        //  showToast(myCursor.getString(arg2));
    	        myCursor.moveToPosition(position);
    	        String path=myCursor.getString(index);
    	        showToast(path);
           
    	   }
       });
    }
	public void showToast(String message)
       {
   		Toast.makeText(this,message , Toast.LENGTH_SHORT).show();
       }
	 public boolean onCreateOptionsMenu(Menu menu) {
	    	// TODO Auto-generated method stub
	    	//return super.onCreateOptionsMenu(menu);
	    	 menu.add(0, menu_Logout, 3,"Đăng Xuất").setIcon(R.drawable.ic_launcher);
	    	 menu.add(0, menu_Backup, 2,"Sao Lưu").setIcon(R.drawable.ic_launcher);
	    	 menu.add(0, menu_MarkAll,1, "Chon Hết").setIcon(R.drawable.ic_launcher);
			return true;
	    }
	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	    	// TODO Auto-generated method stub
	    	switch(item.getItemId())
	    	{
	    		case menu_Backup:
	    			//xu ly backup
	    			// xoa mang item sms 
	    			itemSMSString.clear();
	    			String path= null;
	    			for(int i=0;i<itemSMSCheck.size();i++)
	    			{
	    				// kiem tra mang item check , neu la
	    				//true thi lay vi tri va truyen vao 
	    				//cursor
	    				if(itemSMSCheck.get(i)==true)
	    				{
	    	    	        int index = myCursor.getColumnIndexOrThrow("body");   	        
	    	    	        myCursor.moveToPosition(i);
	    	    	        path=myCursor.getString(index);
	    	    	        //add vao mang itemSMSString
	    					itemSMSString.add(path);
	    					Log.d("Chuoi item", path); 
	    					showToast(path);
	    				}
	    			}
	    			return true;
	    		case menu_Logout:
	    			//xu ly logout
	    			finish();
	    			return true;
	    		case menu_MarkAll:
	    			//xu ly check all
	    			for(int i=0;i<itemSMSCheck.size();i++)
	    		       {
	    				  itemSMSCheck.set(i, true);
	    		    	  mAdapter.setCheckList(itemSMSCheck);
	    		    	  
	    		       }
	    			this.setListAdapter(mAdapter);
	    			return true;
	    	}
	    	return false;
	    }
}